package com.xenos.myPoker;

import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/**
 * 不洗牌，随机取牌
 * 
 * @author Administrator
 *
 */
public class GameMainBeta2 {
	private List<Player> playerList;
	private List<Poker> pokerList; // 存放一副卡牌（未洗）
	private int definePlayerNum;
	private int defineHandCardNum;// 每个玩家要抽取的卡牌张数
	Scanner scanner;

	public GameMainBeta2() {
		definePlayerNum = 3;
		defineHandCardNum = 5;
		pokerList = Poker.makePoker();// Poker用静态方法创建
		playerList = new ArrayList<>();
		scanner = new Scanner(System.in);
	}

	public GameMainBeta2(int definePlayerNum, int defineHandCardNum) {
		this.definePlayerNum = definePlayerNum;
		this.defineHandCardNum = defineHandCardNum;
		pokerList = Poker.makePoker();
		playerList = new ArrayList<>();
		scanner = new Scanner(System.in);
	}

	/**
	 * 创建玩家
	 */
	public void createPlayers() {
		int id;
		String name;
		int playerNum = definePlayerNum; // 循环变量
		int index = 1; // 创建第几位玩家
		while (playerNum > 0) {
			System.out.println("开始创建第" + index + "位玩家");
			System.out.println("请输入玩家编号:");
			try {
				id = scanner.nextInt();
				if (index > 1 && playerList.get(playerList.size() - 1).getID() == id) { // 控制重复id
					System.out.println("编号重复，请重新输入！");
					continue;
				}
			} catch (InputMismatchException e) {// 输入编号的格式异常处理
				System.out.println("非int编号，请重新输入！");
				scanner.next(); // 【吸收缓冲区】不然会死循环
				continue;
			}
			System.out.println("输入玩家姓名:");
			name = scanner.next();
			playerList.add(new Player(id, name)); // 添加玩家从这里可以看出复用性了！先不急于填充域，都获取再填充。
			System.out.println("第" + index + "位玩家创建成功！");
			playerNum--;
			index++;
		}
	}

	/**
	 * 洗牌发牌(利用发牌随机) 要给各自player建造一个私有list存放随机取出的卡牌playerPokerList（Player的构造已给出）；
	 * 从playerList中取出player，调用他的playerPokerList属性的add方法加入卡牌；
	 */
	public void playerGetPoker() {
		Random random = new Random();
		int playerNum = definePlayerNum;
		int playerIndex = 0;// 指示当前读取玩家index
		int pokerNum = 52;// 配合playerNum每个player抽牌循环减少
		int pokerIndex = 0;// 随机抽到的牌的index

		// 前半部循环为一个player抽一张牌，当抽满两张后给下一个抽
		while (playerNum > 0) {
			pokerIndex = random.nextInt(pokerNum);
			// 利用remove【取出】元素解决抽到重复牌的问题 【E remove(index)】
			playerList.get(playerIndex).getPlayerPokerList().add(pokerList.remove(pokerIndex));
			pokerNum--;

			// 限定为给一个player抽牌，没抽够defineHandCardNum时一直抽
			if (!(playerList.get(playerIndex).getPlayerPokerList().size() == defineHandCardNum)) {
				continue;
			}

			playerNum--;
			playerIndex++;
		}
	}

	/**
	 * 用Collections.max方法，根据对应类的compare方法排序
	 * 利用Collections.max取allPlayersMaxPoker中的最大牌
	 */
	public void gameCompare() {
		List<Poker> allPlayersMaxPoker = new ArrayList<>(); // 多了一份空间开销
		// 用【Collections.max】，取各playerPokerList的大牌加入大牌表allPlayersMaxPoker
		for (int i = 0; i < definePlayerNum; i++) {
			allPlayersMaxPoker.add(Collections.max(playerList.get(i).getPlayerPokerList()));// 取大牌加入大牌表
			System.out.println("玩家 " + playerList.get(i).getName() + " 最大的卡牌为： "
					+ allPlayersMaxPoker.get(allPlayersMaxPoker.size() - 1));
		}
		// 利用【Collections.max】取大牌表allPlayersMaxPoker中的最大牌
		Poker maxPoker = Collections.max(allPlayersMaxPoker);
		System.out.println("最大牌为： " + maxPoker);
		for (int i = 0; i < definePlayerNum; i++) {
			if (playerList.get(i).getPlayerPokerList().contains(maxPoker)) {
				System.out.println("------恭喜玩家 " + playerList.get(i).getName() + " 获胜------");
			}
		}
	}

	/**
	 * 打印玩家占有的手牌
	 */
	public void printPlayerPoker() {
		for (int j = 0; j < definePlayerNum; j++) {
			System.out.println(
					"玩家 " + playerList.get(j).getName() + " 有以下卡牌:  " + playerList.get(j).getPlayerPokerList());
		}
	}

	/**
	 * 复用：1.增加游戏玩家;2.增加持有牌数；
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("-------------创建扑克牌-------------");
		GameMainBeta2 gameMainBeta2;
		if (args.length == 0) {
			gameMainBeta2 = new GameMainBeta2();
		} else if (args.length == 2) {
			// 参数设置异常处理，人数大于1小于等于52，取牌数大于0小于等于26,抽到的总牌数不超过52
			if ((Integer.parseInt(args[0]) <= 1) || (Integer.parseInt(args[0]) > 52) || (Integer.parseInt(args[1]) < 1)
					|| (Integer.parseInt(args[1]) > 26)
					|| (Integer.parseInt(args[0]) * Integer.parseInt(args[1]) > 52)) {
				System.out.println("*****参数设置错误，请重新设置玩家数或取牌数*****");
				return;
			}
			gameMainBeta2 = new GameMainBeta2(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
		} else {
			System.out.println("Usuage: java BetaGameMain [definePlayerNum] [defineHandCardNum]");
			return;
		}
		System.out.println("-------------创建玩家------------");
		gameMainBeta2.createPlayers();
		System.out.println("------------玩家随机抽牌----------");
		gameMainBeta2.playerGetPoker();
		System.out.println("--------------比较大小------------");
		gameMainBeta2.gameCompare();
		System.out.println("--------玩家各自的手牌为--------");
		gameMainBeta2.printPlayerPoker();
	}
}
